﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;

namespace LCWMS
{
    public partial class w_so000 : DockContent
    {
        public w_so000()
        {
            InitializeComponent();
            dg_pre_header.AutoGenerateColumns = false;
            dgv_detail.AutoGenerateColumns = false;

        }
        private string str_type = "0";
        private DataSet ds_check;
        private bool b_supply;
        private int w_op_type; //0没有操作，1新增，2修改
        private bool selectmessage;//点查询的时候不触发selection事件
        private bool b_customer = true;
        private DataSet dscustomer;
        private DataSet ds_dept;
        private DataSet ds_reason;
        #region  自定义函数
        private void f_display_detail(string str_consignor, string str_eas)
        {

            string str_sql = " select '0' check_d, eas_no,eas_line_no " +
                               " ,work_line,a.sku,a.pack_code,plan_so_qty_m " +
                               " ,plan_so_qty_s,order_price,order_unit,plan_cubage " +
                               " ,plan_gross_weight,plan_net_weight,a.main_sub_flag,a.convert_modulus " +
                               " ,lot_attribute1,lot_attribute2,ltrim(rtrim(lot_attribute3)) lot_attribute3,lot_attribute4 " +
                               " ,lot_attribute5,lot_attribute6,lot_attribute7,lot_attribute8 " +
                               " ,lot_attribute9,lot_attribute10,lot_attribute11,lot_attribute12 " +
                               " ,lot_attribute13,lot_attribute14,lot_attribute15,eas_sale_no " +
                               " ,sale_line_no,b.sku_name,b.cubage cubage_sku,b.gross_weight gross_weight_sku,b.net_weight net_weight_sku " +
                               " from " + main.g_space + ".so_pre_ship_detail a," + main.g_space + ".mst_sku b   where a.inv_org='" + main.g_org + "' and a.consignor='" + str_consignor + "' and a.eas_no='" + str_eas + "' and a.inv_org = b.inv_org and a.consignor = b.consignor and a.sku = b.sku order by a.eas_line_no";
            DataSet ds_temp;
            try
            {
                if (main.dbtype == "SQL")
                    ds_temp = main.dbservice.Sql_exec_selectsql(str_sql);
                else
                    ds_temp = main.dbservice.oracle_exec_selectsql(str_sql);

                dgv_detail.ReadOnly = false;
                dgv_detail.DataSource = ds_temp.Tables[0];
                if (dgv_detail.Rows.Count > 0)
                {
                    for (int i = 0; i < dgv_detail.Rows.Count; i++)
                    {


                        dgv_detail.Rows[i].Cells["sku"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["sku_name"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["eas_line_no"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["order_unit"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["plan_cubage"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["plan_gross_weight"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["plan_net_weight"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["convert_modulus"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["consignor_"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute1"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute2"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute3"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute4"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute5"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute6"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute7"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute8"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute9"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute10"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute11"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute12"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute13"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute14"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute15"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["eas_no_"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["plan_so_qty_m"].ReadOnly = true;
                    }
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
            }

        }

        //显示datagridview 行号
        private void f_displaypage(Label lbpage, DataGridView dgv)
        {
            int count = dgv.Rows.Count;
            int index = 0;
            if (count > 0)
                index = dgv.CurrentRow.Index + 1;

            if (dgv.SelectedRows.Count == 0)
                index = 0;

            lbpage.Text = index.ToString() + "/" + count.ToString();
            if (dgv.Rows.Count == 0)
            {
                lbpage.Text = "";
            }
        }


        //自定义函数区///////////////////////////////////////////////////////////
        //清空表头信息
        private void f_clearsoheader()
        {
            cbx_newcustomer.Text = "";
            tb_newcusname.Text = "";
            cbx_newsotype.Text = main.g_class.comm_getchangestring("outbound", main.g_info_dataset);
            tb_new_easno.Text = "";
            tb_newexsale.Text = "";
            tb_neworgorder.Text = "";
            cbx_cuscode.SelectedValue = "";
            tb_address.Text = "";
            tb_tele.Text = "";
            tb_receiveday.Text = "";
            tb_receiver.Text = "";
            dtp_sodate.Value = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd").Trim());
            tb_prior.Text = "";
            cbx_rush.Text = main.g_class.comm_getchangestring("Normal", main.g_info_dataset);
            cbx_cross.Text = main.g_class.comm_getchangestring("No", main.g_info_dataset);
            tb_maker.Text = main.g_username;
            tb_memo.Text = "";
            cbx_make_flag.Text = "";
            cbx_vehicle_flag.Text = "";
            cbx_invoice_flag.Text = "";
            cbx_make_flag.Text = "";
            cbx_vehicle_flag.Text = "";
            cbx_invoice_flag.Text = "";

        }


        //设置按钮可用和不可用,b_tmp=true说明可以编辑，否处于待保存状态
        private void f_setbutton(string str_tmp, bool b_tmp)
        {
            if (b_tmp)
            {
                btn_new.Enabled = true;
                btn_modi.Enabled = true;
                btn_del.Enabled = true;
                btn_subnew.Enabled = true;
                btn_submodi.Enabled = true;
                btn_subdel.Enabled = true;
                dg_pre_header.Enabled = true;

                btn_subsave.Enabled = false;
                btn_subcancel.Enabled = false;
                btn_save.Enabled = false;
                btn_cancel.Enabled = false;

                //一些输入框不可编辑
                cbx_newcustomer.Enabled = false;
                tb_newexsale.ReadOnly = true;
                tb_neworgorder.ReadOnly = true;
                cbx_newsotype.Enabled = false;
                cbx_make_flag.Enabled = false;
                cbx_vehicle_flag.Enabled = false;
                cbx_invoice_flag.Enabled = false;


                cbx_cuscode.Enabled = false;
                tb_address.ReadOnly = true;
                tb_tele.ReadOnly = true;
                tb_receiveday.ReadOnly = true;
                //




                tb_receiver.ReadOnly = true;
                dtp_sodate.Enabled = false;

                tb_prior.ReadOnly = true;
                cbx_rush.Enabled = false;
                cbx_cross.Enabled = false;

                cbx_make_flag.Enabled = false;
                cbx_vehicle_flag.Enabled = false;
                cbx_invoice_flag.Enabled = false;


                tb_memo.ReadOnly = true;

                comboBox1.Enabled = false;
                comboBox2.Enabled = false;
            }
            else
            {
                btn_new.Enabled = false;
                btn_modi.Enabled = false;
                btn_del.Enabled = false;
                btn_subnew.Enabled = false;
                btn_submodi.Enabled = false;
                btn_subdel.Enabled = false;
                dg_pre_header.Enabled = false;

                if (str_tmp == "header") //说明是新增表头
                {
                    btn_subsave.Enabled = false;
                    btn_subcancel.Enabled = false;

                    btn_save.Enabled = true;
                    btn_cancel.Enabled = true;
                    cbx_newcustomer.Enabled = true;
                    tb_newexsale.ReadOnly = false;
                    tb_neworgorder.ReadOnly = false;
                    cbx_newsotype.Enabled = true;
                    cbx_make_flag.Enabled = true;
                    cbx_vehicle_flag.Enabled = true;
                    cbx_invoice_flag.Enabled = true;

                    cbx_cuscode.Enabled = true;
                    tb_address.ReadOnly = false;
                    tb_tele.ReadOnly = false;
                    tb_receiveday.ReadOnly = false;
                    //

                    tb_receiver.ReadOnly = false;
                    dtp_sodate.Enabled = true;

                    tb_prior.ReadOnly = false;
                    cbx_rush.Enabled = true;
                    cbx_cross.Enabled = true;
                    tb_memo.ReadOnly = false;
                    comboBox1.Enabled = true;
                    comboBox2.Enabled = true;

                    cbx_make_flag.Enabled = true;
                    cbx_vehicle_flag.Enabled = true;
                    cbx_invoice_flag.Enabled = true;
                }
                else //新增明细
                {
                    btn_subsave.Enabled = true;
                    btn_subcancel.Enabled = true;
                    btn_save.Enabled = false;
                    btn_cancel.Enabled = false;


                }
            }
        }

        //显示表头信息，包括明细的datagridview
        private void f_displayheader(string str_tmp1, string str_tmp2)
        {
            DataSet ds_tmp;
            string str_temp;
            str_temp = "select * from " + main.g_space + ".so_pre_ship_header where inv_org = '" + main.g_org + "' and consignor = '" + str_tmp1 + "' and eas_no = '" + str_tmp2 + "'";
            try
            {
                if (main.dbtype == "SQL")
                    ds_tmp = main.dbservice.Sql_exec_selectsql(str_temp);
                else
                    ds_tmp = main.dbservice.oracle_exec_selectsql(str_temp);

                if (ds_tmp.Tables[0].Rows.Count > 0)
                {
                    //这个函数的调用说明肯定有表头信息，所以不需要判断是否rows>0,直接给数据
                    cbx_newcustomer.Text = ds_tmp.Tables[0].Rows[0]["consignor"].ToString().Trim();
                    tb_newcusname.Text = main.g_class.comm_findname(cbx_newcustomer.Text.Trim(), dscustomer);
                    switch (ds_tmp.Tables[0].Rows[0]["so_type"].ToString().Trim())
                    {
                        case "0":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("Outbound", main.g_info_dataset);
                            break;
                        case "1":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("Return", main.g_info_dataset);
                            break;
                        case "2":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("Consignment", main.g_info_dataset);
                            break;
                        case "3":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("Appropriation", main.g_info_dataset);
                            break;
                        case "4":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("Sale", main.g_info_dataset);
                            break;
                        case "5":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("Other", main.g_info_dataset);
                            break;
                        case "6":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("delegate", main.g_info_dataset);
                            break;
                        case "7":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("outsourcing", main.g_info_dataset);
                            break;
                        case "8":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("guarantees", main.g_info_dataset);
                            break;
                        case "9":
                            cbx_newsotype.Text = main.g_class.comm_getchangestring("Consignment Move", main.g_info_dataset);
                            break;
                        default:
                            cbx_newsotype.Text = "UNKNOWN";
                            break;
                    }
                    tb_new_easno.Text = ds_tmp.Tables[0].Rows[0]["eas_no"].ToString().Trim();
                    tb_newexsale.Text = ds_tmp.Tables[0].Rows[0]["customer_sale_no"].ToString().Trim();
                    tb_neworgorder.Text = ds_tmp.Tables[0].Rows[0]["original_order_no"].ToString().Trim();
                    b_customer = false;
                    cbx_cuscode.SelectedValue = ds_tmp.Tables[0].Rows[0]["customer_code"].ToString().Trim();
                    b_customer = true;
                    tb_address.Text = ds_tmp.Tables[0].Rows[0]["address"].ToString().Trim();
                    tb_tele.Text = ds_tmp.Tables[0].Rows[0]["telephone"].ToString().Trim();
                    tb_receiveday.Text = ds_tmp.Tables[0].Rows[0]["receive_day"].ToString().Trim();


                    tb_receiver.Text = ds_tmp.Tables[0].Rows[0]["receiver"].ToString().Trim();
                    dtp_sodate.Value = System.Convert.ToDateTime(ds_tmp.Tables[0].Rows[0]["so_day"].ToString().Trim());


                    tb_prior.Text = ds_tmp.Tables[0].Rows[0]["so_prior"].ToString().Trim();
                    switch (ds_tmp.Tables[0].Rows[0]["rush_flag"].ToString().Trim())
                    {
                        case "0":
                            cbx_rush.Text = main.g_class.comm_getchangestring("Normal", main.g_info_dataset);
                            break;
                        case "1":
                            cbx_rush.Text = main.g_class.comm_getchangestring("Instancy", main.g_info_dataset);
                            break;
                    }
                    switch (ds_tmp.Tables[0].Rows[0]["cross_flag"].ToString().Trim())
                    {
                        case "0":
                            cbx_cross.Text = main.g_class.comm_getchangestring("No", main.g_info_dataset);
                            break;
                        case "1":
                            cbx_cross.Text = main.g_class.comm_getchangestring("Yes", main.g_info_dataset);
                            break;
                    }
                    tb_maker.Text = ds_tmp.Tables[0].Rows[0]["maker"].ToString().Trim();
                    tb_memo.Text = ds_tmp.Tables[0].Rows[0]["memo"].ToString().Trim();
                    cbx_inv.SelectedValue = ds_tmp.Tables[0].Rows[0]["to_inv_org"].ToString().Trim();
                    comboBox1.Text = ds_tmp.Tables[0].Rows[0]["standby_2"].ToString().Trim();
                    comboBox2.Text = ds_tmp.Tables[0].Rows[0]["standby_3"].ToString().Trim();
                    if (ds_tmp.Tables[0].Rows[0]["make_flag"].ToString().Trim() == "1")
                    {
                        cbx_make_flag.SelectedIndex = 1;
                    }
                    else
                    {
                        cbx_make_flag.SelectedIndex = 0;
                    }
                    if (ds_tmp.Tables[0].Rows[0]["vehicle_flag"].ToString().Trim() == "1")
                    {
                        cbx_vehicle_flag.SelectedIndex = 1;
                    }
                    else
                    {
                        cbx_vehicle_flag.SelectedIndex = 0;
                    }
                    if (ds_tmp.Tables[0].Rows[0]["invoice_flag"].ToString().Trim() == "1")
                    {
                        cbx_invoice_flag.SelectedIndex = 1;
                    }
                    else
                    {
                        cbx_invoice_flag.SelectedIndex = 0;
                    }



                    //显示出库单商品

                    f_display_detail(str_tmp1, str_tmp2);
                }
                else
                {
                    f_clearsoheader();
                    dgv_detail.DataSource = null;
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
        }

        #endregion
        private void w_so000_Load(object sender, EventArgs e)
        {
            DataSet f_ds = new DataSet();
            dtp_start.CustomFormat = "yyyy.MM.dd";
            dtp_start.Format = DateTimePickerFormat.Custom;
            dtp_end.CustomFormat = "yyyy.MM.dd";
            dtp_end.Format = DateTimePickerFormat.Custom;
            dtp_start.Value = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd").Trim());
            dtp_end.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(7).ToString("yyyy.MM.dd").Trim());
            selectmessage = false;
            try
            {
                dscustomer = main.g_class.comm_get_consignor();

                DataRow drnew;
                drnew = dscustomer.Tables[0].NewRow();
                drnew["consignor"] = "";
                drnew["consignor_name"] = "";
                dscustomer.Tables[0].Rows.Add(drnew);
                cbx_customer.ValueMember = "consignor";
                cbx_customer.DisplayMember = "consignor";
                cbx_customer.DataSource = dscustomer.Tables[0];
                if (dscustomer.Tables[0].Rows.Count > 0) tb_custname.Text = dscustomer.Tables[0].Rows[0][1].ToString().Trim();



                cbx_newcustomer.ValueMember = "consignor";
                cbx_newcustomer.DisplayMember = "consignor";
                cbx_newcustomer.DataSource = dscustomer.Tables[0].Copy();
                cbx_newcustomer.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            //订单类型
            try
            {
                f_ds = main.g_class.comm_get_dictionary("so_type");
                //进行语言转换
                int i_ds;
                i_ds = f_ds.Tables[0].Rows.Count;
                if (i_ds > 0)
                {
                    for (int i = 0; i < i_ds; i++)
                    {
                        f_ds.Tables[0].Rows[i]["so_type"] = main.g_class.comm_getchangestring(f_ds.Tables[0].Rows[i]["so_type"].ToString().Trim(), main.g_info_dataset);
                    }
                }
                ds_check = new DataSet();
                ds_check = f_ds.Copy();
                DataSet newf_ds = f_ds.Copy();
                DataRow drnew;
                drnew = f_ds.Tables[0].NewRow();
                drnew["so_type"] = "";
                f_ds.Tables[0].Rows.Add(drnew);
                cbx_sotype.ValueMember = "so_type";
                cbx_sotype.DisplayMember = "so_type";
                cbx_sotype.DataSource = f_ds.Tables[0];
                cbx_sotype.Text = "";

                cbx_newsotype.ValueMember = "so_type";
                cbx_newsotype.DisplayMember = "so_type";
                cbx_newsotype.DataSource = f_ds.Tables[0].Copy();
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of bill's type", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }

            //初始化部门
            ds_dept = main.g_class.comm_get_dictionary("department");
            DataRow drnew2 = ds_dept.Tables[0].NewRow();
            drnew2[0] = "";
            ds_dept.Tables[0].Rows.Add(drnew2);
            comboBox1.ValueMember = "department";
            comboBox1.DisplayMember = "department";
            comboBox1.DataSource = ds_dept.Tables[0];
            //初始化领料原因
            ds_reason = main.g_class.comm_get_dictionary("take_reason");
            //DataRow drnew1 = ds_reason.Tables[0].NewRow();
            //drnew1[0] = "";
            //ds_reason.Tables[0].Rows.Add(drnew1);
            for (int i = 0; i < ds_reason.Tables[0].Rows.Count; i++)
            {
                comboBox2.Items.Add(ds_reason.Tables[0].Rows[i]["take_reason"].ToString().Trim());
            }

            //日期控件默认从今天到一个星期
            //dtp_start.CustomFormat = "yyyy.MM.dd HH:mm:ss";
            dtp_start.CustomFormat = "yyyy.MM.dd";
            dtp_start.Format = DateTimePickerFormat.Custom;
            dtp_end.CustomFormat = "yyyy.MM.dd";
            dtp_end.Format = DateTimePickerFormat.Custom;
            dtp_start.Value = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd").Trim());
            dtp_end.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(7).ToString("yyyy.MM.dd").Trim());
            dtp_sodate.CustomFormat = "yyyy.MM.dd";
            dtp_sodate.Format = DateTimePickerFormat.Custom;
            dtp_sodate.Value = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd").Trim());

            //获取按钮权限
            string str_right;
            str_right = main.g_class.comm_getright("w_so000", main.g_right_dataset);
            //分单
            if (str_right.Substring(4, 1) == "0")
                btn_fendan.Visible = false;
            else
                btn_fendan.Visible = true;


            DataTable dt_dgso = new DataTable();

            dt_dgso.Columns.Add("dist");
            dt_dgso.Columns.Add("so_no");
            dt_dgso.Columns.Add("consignor");
            dt_dgso.Columns.Add("eas_no");
            dt_dgso.Columns.Add("so_type");
            dt_dgso.Columns.Add("customer_sale_no");
            dt_dgso.Columns.Add("so_day");
            dt_dgso.Columns.Add("so_status");
            dg_pre_header.DataSource = dt_dgso;

            cbx_so_status.SelectedIndex = 0;
            selectmessage = true;
            //语言转换
            main.g_class.comm_changeControl(this, main.g_win_dataset);
        }

        private void cbx_customer_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_customer.Text.Trim();
            tb_custname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            string str_sql = "", str_sotype = "";
            DataSet ds_query;
            str_sql = "select 0 dist,a.consignor,a.eas_no,case a.so_type when '0' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' " +
                         "when '1' then '" + main.g_class.comm_getchangestring("Return", main.g_info_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("Consignment ", main.g_info_dataset) + "' when '3' then '" +
                          main.g_class.comm_getchangestring("Appropriation", main.g_info_dataset) + "' when '4' then '" + main.g_class.comm_getchangestring("Sale", main.g_info_dataset) + "' when '5' then '" +
                          main.g_class.comm_getchangestring("Other", main.g_info_dataset) + "' when '6' then '" + main.g_class.comm_getchangestring("delegate", main.g_info_dataset) + "'  when '7' then '" + main.g_class.comm_getchangestring("outsourcing", main.g_info_dataset) + "' when '8' then '" + main.g_class.comm_getchangestring("guarantees", main.g_info_dataset) + "' else '" +
                          main.g_class.comm_getchangestring("CONSIGNMENT MOVE", main.g_info_dataset) + "' end as so_type,a.customer_sale_no ,a.so_day,case a.so_status when '0' then '生成' when '1' then '分单' else '' end so_status From " + main.g_space + ".so_pre_ship_header a where a.inv_org='" + main.g_org + "'";
            //领料单号
            if (tb_eas_no.Text.Trim() != "")
            {
                str_sql += " and a.eas_no = '" + tb_eas_no.Text.Trim() + "'";
            }
            //判断订单类型
            str_sotype = main.g_class.comm_getconversestring(cbx_sotype.Text.Trim(), main.g_info_dataset);
            switch (str_sotype.Trim().ToUpper())
            {
                case "OUTBOUND":
                    str_sotype = "0";
                    break;
                case "RETURN":
                    str_sotype = "1";
                    break;
                case "CONSIGNMENT":
                    str_sotype = "2";
                    break;
                case "APPROPRIATION":
                    str_sotype = "3";
                    break;
                case "SALE":
                    str_sotype = "4";
                    break;
                case "OTHER":
                    str_sotype = "5";
                    break;
                case "DELEGATE":
                    str_sotype = "6";
                    break;
                case "OUTSOURCING":
                    str_sotype = "7";
                    break;
                case "GUARANTEES":
                    str_sotype = "8";
                    break;
                case "CONSIGNMENT MOVE":
                    str_sotype = "9";
                    break;
                default:
                    str_sotype = "*";
                    break;
            }
            if (str_sotype != "*")
            {
                str_sql = str_sql + " and a.so_type = '" + str_sotype + "'  ";
            }
            //工作令
            if (tb_work_no.Text.Trim() != "")
            {
                str_sql += " and a.customer_sale_no = '" + tb_work_no.Text.Trim() + "'";
            }
            if (cbx_so_status.Text.Trim() != "")
            {
                if (cbx_so_status.Text.Trim() == "生成")
                {
                    str_sql += " and a.so_status='0'";
                }
                else if (cbx_so_status.Text.Trim() == "分单")
                {
                    str_sql += " and a.so_status='1'";
                }
            }
            //仓库管理员
            if (tb_warehouseman.Text.Trim() != "")
            {
                str_sql += " and exists (select 1 from " + main.g_space + ".so_pre_ship_detail b," + main.g_space + ".mst_sku c where a.inv_org=b.inv_org and b.inv_org=c.inv_org and a.consignor=b.consignor " +
                               "and b.consignor=c.consignor and a.eas_no=b.eas_no and b.sku=c.sku and c.warehouseman = '" + tb_warehouseman.Text.Trim() + "')";
            }
            //判断日期
            str_sql += " and a.so_day between '" + dtp_start.Text.ToString().Trim() + "' and '" + dtp_end.Text.ToString().Trim() + "' order by a.eas_no";
            try
            {
                if (main.dbtype == "SQL")
                    ds_query = main.dbservice.Sql_exec_selectsql(str_sql);
                else
                    ds_query = main.dbservice.oracle_exec_selectsql(str_sql);

                dg_pre_header.DataSource = ds_query.Tables[0];
                f_displaypage(lblpage, dg_pre_header);
                if (dg_pre_header.Rows.Count <= 0)
                {
                    f_display_detail("", "");
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
            }
        }

        private void dg_pre_header_SelectionChanged(object sender, EventArgs e)
        {
            if (dg_pre_header.CurrentRow != null && selectmessage == true)
            {
                f_displayheader(dg_pre_header.Rows[dg_pre_header.CurrentRow.Index].Cells["consignor"].Value.ToString().Trim(), dg_pre_header.Rows[dg_pre_header.CurrentRow.Index].Cells["eas_no"].Value.ToString().Trim());
                //f_display_detail(dg_pre_header.Rows[dg_pre_header.CurrentRow.Index].Cells["consignor"].Value.ToString().Trim(), dg_pre_header.Rows[dg_pre_header.CurrentRow.Index].Cells["eas_no"].Value.ToString().Trim());
                f_displaypage(lblpage, dg_pre_header);
            }
        }

        private void bt_all1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dg_pre_header.Rows.Count; i++)
            {
                if (this.dg_pre_header.Rows[i].Cells["dist"].Value.ToString() == "1")
                    continue;
                this.dg_pre_header.Rows[i].Cells["dist"].Value = "1";
            }
        }

        private void bt_reset1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dg_pre_header.Rows.Count; i++)
            {
                if (this.dg_pre_header.Rows[i].Cells["dist"].Value.ToString() == "1")
                    this.dg_pre_header.Rows[i].Cells["dist"].Value = "0";
                else
                    this.dg_pre_header.Rows[i].Cells["dist"].Value = "1";
            }
        }

        private void btn_delet_Click(object sender, EventArgs e)
        {

        }

        private void btn_fendan_Click(object sender, EventArgs e)
        {
            string str_consignor = "", str_easno = "", str_Message = "";
            if (MessageBox.Show("确定将选中领料单分单!", "Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                for (int i = 0; i < this.dg_pre_header.Rows.Count; i++)//循环找出要删除用户
                {
                    str_Message = "";
                    if (this.dg_pre_header.Rows[i].Cells["dist"].Value.ToString().Trim() == "1" && this.dg_pre_header.Rows[i].Cells["so_status"].Value.ToString().Trim() == "生成")
                    {
                        str_consignor = dg_pre_header.Rows[i].Cells["consignor"].Value.ToString().Trim();
                        str_easno = dg_pre_header.Rows[i].Cells["eas_no"].Value.ToString().Trim();
                        try
                        {
                            if (main.dbtype == "SQL")
                                str_Message = main.dbservice.Sql_exec_proc_sobomsplit_new(main.g_org, str_consignor, str_easno, main.g_user);
                            else
                                str_Message = main.dbservice.oracle_exec_proc_billsplit(main.g_org, str_consignor, str_easno, main.g_user);
                        }
                        catch (System.Exception ee)
                        {
                            MessageBox.Show("分单失败 " + ee.Message);
                            btn_query_Click(sender, new EventArgs());
                            return;
                        }
                        if (str_Message.Trim() != "")
                        {
                            MessageBox.Show(str_easno + " 分单失败!" + str_Message);
                            btn_query_Click(sender, new EventArgs());
                            return;
                        }
                    }
                }
                btn_query_Click(sender, new EventArgs());
            }
        }

        private void dg_pre_header_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dg_pre_header.CurrentRow != null) //说明选择了行号
            {
                //选择长度和数量可以编辑,否则不能编辑
                if (dg_pre_header.Rows[dg_pre_header.CurrentRow.Index].Cells["so_status"].Value.ToString().Trim() == "分单")
                {
                    this.dg_pre_header.Rows[dg_pre_header.CurrentRow.Index].Cells["dist"].ReadOnly = true;
                }
                else
                {
                    this.dg_pre_header.Rows[dg_pre_header.CurrentRow.Index].Cells["dist"].ReadOnly = false;
                }
            }
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            selectmessage = false;
            b_customer = false;
            w_op_type = 1;
            //清空表头信息
            f_clearsoheader();
            //清空明细信息

            dgv_detail.DataSource = null;
            //清空明细gridview
            //     if (dg_asndetail.RowCount > 0) dg_asndetail.Rows.c
            //控制按钮不可用
            f_setbutton("header", false);
            selectmessage = true;
            b_customer = true;
            comboBox1.Text = "";
            if (cbx_newcustomer.Items.Count == 1)
            {
                cbx_newcustomer.Text = cbx_customer.Text.Trim();
            }
            
            cbx_newcustomer.SelectedIndex = 0;
            cbx_make_flag.SelectedIndex = 0;
            cbx_vehicle_flag.SelectedIndex = 0;
            cbx_invoice_flag.SelectedIndex = 0;
        }

        private void btn_modi_Click(object sender, EventArgs e)
        {
            if (tb_new_easno.Text.Trim() == "") //说明没有选择出库单
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please Choose a bill which need to modify", main.g_info_dataset) + "!");
                return;
            }
            w_op_type = 2;
            f_setbutton("header", false);
             
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (tb_new_easno.Text.Trim() == "")
            {
                MessageBox.Show("未选择正确的单据!");
                return;
            }

            string[] str_sql = new string[2];
            if (MessageBox.Show("确定要删除该单据!", "Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {

                str_sql[0] = "delete " + main.g_space + ".so_pre_ship_header where inv_org='" + main.g_org + "' and consignor='" + cbx_newcustomer.Text.Trim() + "' and  eas_no ='" + tb_new_easno.Text.Trim() + "'";
                str_sql[1] = "delete " + main.g_space + ".so_pre_ship_detail where inv_org='" + main.g_org + "' and consignor='" + cbx_newcustomer.Text.Trim() + "' and  eas_no ='" + tb_new_easno.Text.Trim() + "'";

                try
                {
                    if (main.dbtype == "SQL")
                        main.dbservice.Sql_exec_mutilsql(str_sql);
                    else
                        main.dbservice.oracle_exec_mutilsql(str_sql);
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show("删除失败 " + ee.Message);
                }
                btn_query_Click(sender, new EventArgs());

            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string str_newcustom, str_temp, str_newtype, str_prior, str_rush, str_cross, str_newso;
            string str_check, str_cuscode = "";
            string str_trans_inv;
            string str_make_flag = "", str_vehicle_flag = "", str_invoice_flag = "";
            int i_tmp;
            str_temp = "";

            str_trans_inv = "";

            //判断货主知否合法 cbx_newcustomer
            if (tb_newcusname.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Consignor is not exists or haven't privilege", main.g_info_dataset) + "!");
                return;
            }
            if (cbx_newcustomer.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please choose a consignor", main.g_info_dataset) + "!");
                return;
            }
            if (cbx_make_flag.Text.Trim() == "是")
            {
                str_make_flag = "1";
            }
            else
            {
                str_make_flag = "0";
            }
            if (cbx_vehicle_flag.Text.Trim() == "是")
            {
                str_vehicle_flag = "1";
            }
            else
            {
                str_vehicle_flag = "0";
            }
            if (cbx_invoice_flag.Text.Trim() == "是")
            {
                str_invoice_flag = "1";
            }
            else
            {
                str_invoice_flag = "0";
            }
           
            //判断原订单号是否存在
            if (tb_neworgorder.Text.Trim() != "")
            {
                str_temp = "select count(*) from " + main.g_space + ".asn_order_header where inv_org = '" + main.g_org + "' and consignor = '" + cbx_newcustomer.Text.Trim() + "' and asn_no = '" + tb_neworgorder.Text.Trim() + "'";
                try
                {
                    if (main.dbtype == "SQL")
                        i_tmp = main.dbservice.Sql_exec_selectint(str_temp);
                    else
                        i_tmp = main.dbservice.oracle_exec_selectint(str_temp);
                    if (i_tmp <= 0) //说明入库单不合法
                    {
                        MessageBox.Show(main.g_class.comm_getchangestring("Incorrect ASN bill", main.g_info_dataset) + ":" + tb_neworgorder.Text.Trim() + "!");
                        return;
                    }
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to check the ASN bill", main.g_info_dataset) + ":" + tb_neworgorder.Text.Trim() + "!" + ee.ToString());
                    return;
                }
            }
            if (cbx_cuscode.SelectedValue != null)
            {
                str_cuscode = cbx_cuscode.SelectedValue.ToString().Trim();
            }
            DataRow[] dr_check = ds_check.Tables[0].Select("so_type = '" + cbx_newsotype.Text.Trim() + "'");
            if (dr_check.Length > 0)
            {
                if (dr_check[0]["content2"].ToString().Trim() == "1")
                {
                    str_check = "0";
                }
                else
                {
                    str_check = "1";
                }
            }
            else
            {
                str_check = "1";
            }
            str_newtype = main.g_class.comm_getconversestring(cbx_newsotype.Text.Trim(), main.g_info_dataset);
            switch (str_newtype.Trim().ToUpper())
            {
                case "OUTBOUND":
                    str_newtype = "0";
                    break;
                case "RETURN":
                    str_newtype = "1";
                    break;
                case "CONSIGNMENT":
                    str_newtype = "2";
                    break;
                case "APPROPRIATION":
                    str_newtype = "3";
                    break;
                case "SALE":
                    str_newtype = "4";
                    break;
                case "OTHER":
                    str_newtype = "5";
                    break;
                case "DELEGATE":
                    str_newtype = "6";
                    break;
                case "OUTSOURCING":
                    str_newtype = "7";
                    break;
                case "GUARANTEES":
                    str_newtype = "8";
                    break;
                case "CONSIGNMENT MOVE":
                    str_newtype = "9";
                    break;
                default:
                    str_newtype = "";
                    break;
            }
            if (str_newtype == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please choose bill's type", main.g_info_dataset) + "!");
                return;
            }
            //如果是调拨
            if (str_newtype == "3")
            {
                if (cbx_inv.Text.Trim() == "")
                {
                    MessageBox.Show("入库库存组织不能为空!");
                    return;
                }
                else
                {
                    str_trans_inv = cbx_inv.SelectedValue.ToString().Trim();
                }
            }

            if (str_newtype.Trim() == "9")
            {
                if (cbx_inv.Text.Trim() == "")
                {
                    MessageBox.Show("货主不能为空!");
                    return;
                }
                else
                {
                    if (cbx_newcustomer.Text.Trim() == cbx_inv.SelectedValue.ToString().Trim())
                    {
                        MessageBox.Show("货主不能重复！");
                        cbx_inv.Focus();
                        return;
                    }
                    str_trans_inv = cbx_inv.SelectedValue.ToString().Trim();
                }
            }
            if (tb_prior.Text.Trim() == "")
                str_prior = "0";
            else
                try
                {
                    str_prior = System.Convert.ToInt32(tb_prior.Text.Trim()).ToString().Trim();
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Input incorrect PRI", main.g_info_dataset) + "!" + ee.ToString().Trim());
                    return;
                }
            str_rush = main.g_class.comm_getconversestring(cbx_rush.Text.Trim(), main.g_info_dataset);
            switch (str_rush.Trim().ToUpper())
            {
                case "NORMAL":
                    str_rush = "0";
                    break;
                case "INSTANCY":
                    str_rush = "1";
                    break;
            }
            str_cross = main.g_class.comm_getconversestring(cbx_cross.Text.Trim(), main.g_info_dataset);
            switch (str_cross.Trim().ToUpper())
            {
                case "NO":
                    str_cross = "0";
                    break;
                case "YES":
                    str_cross = "1";
                    break;
            }
            //            tb_receiveday.tex
            DateTime dt = Convert.ToDateTime(dtp_sodate.Text);
            tb_receiveday.Text = dt.AddDays(-1).ToString("yyyy.MM.dd");
            Cursor.Current = Cursors.WaitCursor;
            tb_memo.Text = tb_memo.Text.Trim().Replace("'", "");
            if (w_op_type == 1) //新增  
            {
                //int i_count = 0;
                //str_temp = " select count(*) from " + main.g_space + ".so_pre_ship_header where inv_org='" + main.g_org + "' and consignor='" + cbx_newcustomer.Text.Trim() + "' and eas_no='" + tb_new_easno.Text.Trim() + "'";
                //if (main.dbtype == "SQL")
                //{
                //    i_count = main.dbservice.Sql_exec_selectint(str_temp);
                //}
                //else
                //{
                //    i_count = main.dbservice.oracle_exec_selectint(str_temp);
                //}
                //if (i_count > 0)
                //{
                //    MessageBox.Show("单据号已经存在.");
                //    tb_new_easno.Focus();
                //    return;
                //}

                //得到订单号
                if (str_newtype.Trim() == "1")
                {
                    str_newso = main.g_class.comm_getaccountno("TO_NO");
                }
                else
                {
                    str_newso = main.g_class.comm_getaccountno("so_no");
                }
                if (str_newso.Substring(0, 5) == "error")
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to get new number of bill", main.g_info_dataset) + "!" + str_newso);
                    Cursor.Current = Cursors.Default;
                    return;
                }
                else
                    tb_new_easno.Text = str_newso;
                //插入表头信息
                str_temp = "insert into " + main.g_space + ".so_pre_ship_header(inv_org,            consignor,          eas_no,          customer_sale_no,       customer_code," +
                                                                           "customer_name,      receiver,       telephone,              address,  receive_day," +
                                                                           "so_day,		        rush_flag,	    cross_flag,	            so_type," +
                                                                           "original_order_no,	so_prior,	    print_day,	            printer," +
                                                                           "confirm_day,	    confirmer,	    ship_day,	            shipper," +
                                                                           "wave_flag,	        so_flag,	    so_status,	            so_assign, colse_day," +
                                                                           "colser,		        standby_1,	    standby_2,	            standby_3," +
                                                                           "standby_4,	        standby_5,	    memo,		            main_dist," +
                                                                           "made_time,	        maker,to_inv_org,make_flag,vehicle_flag,invoice_flag) values ('" + main.g_org + "','" + cbx_newcustomer.Text.Trim() + "','" +
                    tb_new_easno.Text.Trim() + "','" + tb_newexsale.Text.Trim() + "','" + str_cuscode + "','" + cbx_cuscode.Text.ToString().Trim() + "','" + tb_receiver.Text.Trim() + "','" + tb_tele.Text.Trim() + "','" +
                    tb_address.Text.Trim() + "','" + tb_receiveday.Text.Trim() + "','" + dtp_sodate.Value.ToString("yyyy.MM.dd") + "','" + str_rush + "','" + str_cross + "','" +
                    str_newtype + "','" + tb_neworgorder.Text.Trim() + "'," + str_prior + ",'','','','','','','0','0','0','0','','','','" + comboBox1.Text.Trim() + "','" + comboBox2.Text.Trim() + "'," + str_check + ",0,'" + tb_memo.Text.Trim() + "','A',";
                if (main.dbtype == "SQL")
                {
                    str_temp = str_temp + "getdate(),'";
                }
                else
                {
                    str_temp = str_temp + " sysdate,'";
                }
                str_temp = str_temp + main.g_user + "','" + str_trans_inv.Trim() + "','" + str_make_flag + "','" + str_vehicle_flag + "','" + str_invoice_flag + "')";

            }
            else if (w_op_type == 2) //修改
            {
                //修改表头信息  
                str_newcustom = cbx_newcustomer.Text.Trim();

                str_temp = "update " + main.g_space + ".so_pre_ship_header set customer_sale_no='" + tb_newexsale.Text.Trim() + "',customer_code='" + str_cuscode + "'," +
                    "customer_name = '" + cbx_cuscode.Text.ToString().Trim() + "',receiver='" + tb_receiver.Text.Trim() + "',telephone='" + tb_tele.Text.Trim() + "',address='" + tb_address.Text.Trim() + "',receive_day='" + tb_receiveday.Text.Trim() + "'," +
                    "so_day='" + dtp_sodate.Value.ToString("yyyy.MM.dd") + "',rush_flag='" + str_rush + "',cross_flag='" + str_cross + "',so_type='" + str_newtype + "',standby_4 = " + str_check + "," +
                    "original_order_no='" + tb_neworgorder.Text.Trim() + "',so_prior=" + str_prior + ",standby_2='" + comboBox1.Text.Trim() + "',standby_3='" + comboBox2.Text.Trim() + "',memo='" + tb_memo.Text.Trim() + "',main_dist='C',updater='" + main.g_user + "',to_inv_org = '" + str_trans_inv.Trim() + "',update_time=";
                if (main.dbtype == "SQL")
                {
                    str_temp = str_temp + "getdate() ";
                }
                else
                {
                    str_temp = str_temp + "sysdate ";
                }

                str_temp += ",make_flag='" + str_make_flag + "',vehicle_flag='" + str_vehicle_flag + "',invoice_flag='" + str_invoice_flag + "'";
                str_temp = str_temp + "where inv_org = '" + main.g_org + "' and consignor = '" + str_newcustom + "' and eas_no = '" + tb_new_easno.Text.Trim() + "'";
            }
            try
            {
                if (main.dbtype == "SQL")
                {
                    main.dbservice.Sql_exec_commandsql(str_temp);
                }
                else
                {
                    main.dbservice.oracle_exec_commandsql(str_temp);
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to save bill", main.g_info_dataset) + "!" + ee.ToString());
                Cursor.Current = Cursors.Default;
                return;
            }
            //dgasn添加一行
            if (w_op_type == 1)
            {
                selectmessage = false;
                DataRow newdr = ((DataTable)dg_pre_header.DataSource).NewRow();
                newdr["dist"] = "0";
                newdr["consignor"] = cbx_newcustomer.Text.Trim();
                newdr["eas_no"] = tb_new_easno.Text.Trim();
                newdr["so_type"] = cbx_newsotype.Text.Trim();
                newdr["customer_sale_no"] = tb_newexsale.Text.Trim();
                newdr["so_day"] = dtp_sodate.Value.ToString().Trim();
                newdr["so_status"] = "生成";

                ((DataTable)dg_pre_header.DataSource).Rows.Add(newdr);
                //选中这一行
                dg_pre_header.Rows[dg_pre_header.CurrentRow.Index].Selected = false;
                dg_pre_header.Rows[dg_pre_header.RowCount - 1].Selected = true;
                selectmessage = true;
            }
            f_setbutton("header", true);
            w_op_type = 0;
            Cursor.Current = Cursors.Default;
            cbx_inv.Enabled = false;
            cbx_make_flag.Enabled = false;
            cbx_vehicle_flag.Enabled = false;
            cbx_invoice_flag.Enabled = false;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            //btn_query_Click(sender, new EventArgs());
            w_op_type = 0;
            f_setbutton("header", true);
            cbx_inv.Enabled = false;
            dg_pre_header_SelectionChanged(sender, new EventArgs());
        }

        private void cbx_newcustomer_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_newcustomer.Text.Trim();
            tb_newcusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void btn_show_Click(object sender, EventArgs e)
        {
            w_asn001_supply su = new w_asn001_supply();
            w_asn001_supply.s_type = str_type.Trim();
            su.ShowDialog();
            if (w_asn001_supply.s_consignor != "")
            {
                cbx_cuscode.SelectedValue = w_asn001_supply.s_consignor;
            }
        }

        private void cbx_cuscode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str_sql;
            DataSet ds_temp;
            if (b_customer) //为真时 提取相应的客户信息
            {
                if (cbx_cuscode.SelectedIndex >= 0 && cbx_cuscode.SelectedValue.ToString().Trim() != "")  //合法，去相应的值
                {
                    str_sql = "select business_address,telephone,clerk from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and consignor = '" + cbx_cuscode.SelectedValue.ToString().Trim() + "'";
                    if (main.dbtype == "SQL")
                    {
                        ds_temp = main.dbservice.Sql_exec_selectsql(str_sql);
                    }
                    else
                    {
                        ds_temp = main.dbservice.oracle_exec_selectsql(str_sql);
                    }
                    if (ds_temp.Tables[0].Rows.Count > 0) //说明有数据
                    {
                        tb_address.Text = ds_temp.Tables[0].Rows[0]["business_address"].ToString().Trim();
                        tb_tele.Text = ds_temp.Tables[0].Rows[0]["telephone"].ToString().Trim();
                        tb_receiver.Text = ds_temp.Tables[0].Rows[0]["clerk"].ToString().Trim();
                    }
                    else
                    {
                        tb_address.Text = "";
                        tb_tele.Text = "";
                        tb_receiver.Text = "";
                    }
                }
                else
                {
                    tb_address.Text = "";
                    tb_tele.Text = "";
                    tb_receiver.Text = "";
                    tb_receiveday.Text = "";
                }
            }
        }

        private void cbx_newsotype_TabIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbx_newsotype_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str_sql;
            DataSet ds_supply;
            #region
            if (main.g_class.comm_getconversestring(cbx_newsotype.Text.Trim(), main.g_info_dataset).ToUpper() == "RETURN" && !b_supply) //如果是退货,则客户显示供应商
            {
                str_type = "0";
                //初始化客户编码
                try
                {
                    if (main.dbtype == "SQL")
                    {
                        str_sql = "select ltrim(rtrim(consignor)) consignor,ltrim(rtrim(consignor_name)) consignor_name from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and supply_flag = '1' order by consignor";
                        ds_supply = main.dbservice.Sql_exec_selectsql(str_sql);
                    }
                    else
                    {
                        str_sql = "select trim(consignor) consignor,trim(consignor_name) consignor_name from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and supply_flag = '1' order by consignor";
                        ds_supply = main.dbservice.oracle_exec_selectsql(str_sql);
                    }
                    DataRow drnew;
                    drnew = ds_supply.Tables[0].NewRow();
                    drnew["consignor"] = "";
                    drnew["consignor_name"] = "";
                    ds_supply.Tables[0].Rows.Add(drnew);
                    cbx_cuscode.ValueMember = "consignor";
                    cbx_cuscode.DisplayMember = "consignor_name";
                    cbx_cuscode.DataSource = ds_supply.Tables[0];
                    cbx_cuscode.SelectedValue = "";
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize customer's Information", main.g_info_dataset) + "!" + ee.ToString());
                    return;
                }
                b_supply = true;
            }
            else if (cbx_newsotype.Text.Trim() == "委外加工" || cbx_newsotype.Text.Trim() == "外协加工")
            {
                str_type = "0";
                //初始化客户编码
                try
                {
                    if (main.dbtype == "SQL")
                    {
                        str_sql = "select ltrim(rtrim(consignor)) consignor,ltrim(rtrim(consignor_name)) consignor_name from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and supply_flag = '1' order by consignor";
                        ds_supply = main.dbservice.Sql_exec_selectsql(str_sql);
                    }
                    else
                    {
                        str_sql = "select trim(consignor) consignor,trim(consignor_name) consignor_name from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and supply_flag = '1' order by consignor";
                        ds_supply = main.dbservice.oracle_exec_selectsql(str_sql);
                    }
                    DataRow drnew;
                    drnew = ds_supply.Tables[0].NewRow();
                    drnew["consignor"] = "";
                    drnew["consignor_name"] = "";
                    ds_supply.Tables[0].Rows.Add(drnew);
                    cbx_cuscode.ValueMember = "consignor";
                    cbx_cuscode.DisplayMember = "consignor_name";
                    cbx_cuscode.DataSource = ds_supply.Tables[0];
                    cbx_cuscode.SelectedValue = "";
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize customer's Information", main.g_info_dataset) + "!" + ee.ToString());
                    return;
                }
                b_supply = true;
            }
            else //显示客户
            {
                str_type = "1";
                //初始化客户编码
                try
                {
                    if (main.dbtype == "SQL")
                    {
                        str_sql = "select ltrim(rtrim(consignor)) consignor,ltrim(rtrim(consignor_name)) consignor_name from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and customer_flag = '1' order by consignor";
                        ds_supply = main.dbservice.Sql_exec_selectsql(str_sql);
                    }
                    else
                    {
                        str_sql = "select trim(consignor) consignor,trim(consignor_name) consignor_name from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and customer_flag = '1' order by consignor";
                        ds_supply = main.dbservice.oracle_exec_selectsql(str_sql);
                    }
                    DataRow drnew;
                    drnew = ds_supply.Tables[0].NewRow();
                    drnew["consignor"] = "";
                    drnew["consignor_name"] = "";
                    ds_supply.Tables[0].Rows.Add(drnew);
                    cbx_cuscode.ValueMember = "consignor";
                    cbx_cuscode.DisplayMember = "consignor_name";
                    cbx_cuscode.DataSource = ds_supply.Tables[0];
                    cbx_cuscode.SelectedValue = "";
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize customer's Information", main.g_info_dataset) + "!" + ee.ToString());
                    return;
                }
                b_supply = false;
            }

            if (main.g_class.comm_getconversestring(cbx_newsotype.Text.Trim(), main.g_info_dataset).ToUpper() == "APPROPRIATION" ||
                main.g_class.comm_getconversestring(cbx_newsotype.Text.Trim(), main.g_info_dataset).ToUpper() == "CONSIGNMENT MOVE") //如果是调拨,可用,
            {
                cbx_inv.Enabled = true;
            }
            else
            {
                cbx_inv.Enabled = false;
            }
            #endregion
            if (main.g_class.comm_getconversestring(cbx_newsotype.Text.Trim(), main.g_info_dataset).ToUpper() == "CONSIGNMENT MOVE")
            {
                DataSet ds_inv;
                str_sql = "select a.consignor ,a.consignor_name from " + main.g_space + ".mst_customer a " +
                         " where a.inv_org = '" + main.g_org + "' and a.consignor_flag='1' and a.main_dist <> 'D' order by a.consignor";
                try
                {
                    if (main.dbtype == "SQL")
                        ds_inv = main.dbservice.Sql_exec_selectsql(str_sql);
                    else
                        ds_inv = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show("转货主信息初始化失败!" + ee.ToString());
                    return;
                }
                DataRow drnew;
                cbx_inv.DataSource = null;
                drnew = ds_inv.Tables[0].NewRow();
                drnew["consignor"] = "";
                drnew["consignor_name"] = "";
                ds_inv.Tables[0].Rows.Add(drnew);
                cbx_inv.ValueMember = "consignor";
                cbx_inv.DisplayMember = "consignor";
                cbx_inv.DataSource = ds_inv.Tables[0];
                label25.Text = main.g_class.comm_getchangestring("consignor", main.g_win_dataset);
            }
            else
            {
                DataSet ds_inv;
                //初始化库存组织
                try
                {
                    str_sql = "select inv_org,org_name from " + main.g_space + ".mst_organize where inv_org <> '" + main.g_org + "' order by inv_org";
                    if (main.dbtype == "SQL")
                    {
                        ds_inv = main.dbservice.Sql_exec_selectsql(str_sql);
                    }
                    else
                    {
                        ds_inv = main.dbservice.oracle_exec_selectsql(str_sql);
                    }
                    DataRow drnew;
                    cbx_inv.DataSource = null;
                    drnew = ds_inv.Tables[0].NewRow();
                    drnew["inv_org"] = "";
                    drnew["org_name"] = "";
                    ds_inv.Tables[0].Rows.Add(drnew);
                    cbx_inv.ValueMember = "inv_org";
                    cbx_inv.DisplayMember = "org_name";
                    cbx_inv.DataSource = ds_inv.Tables[0];
                    cbx_inv.SelectedValue = "";
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show("调拨库存组织信息初始化失败!" + ee.ToString());
                    return;
                }
            }
            cbx_inv.Text = "";
        }

        private void btn_subnew_Click(object sender, EventArgs e)
        {
            if (tb_new_easno.Text.Trim() != "")
            {
                try
                {
                    string str_sql = "", str_status = "";
                    str_sql = " select so_status from " + main.g_space + ".so_pre_ship_header where inv_org='" + main.g_org + "' and consignor='" + cbx_newcustomer.Text.Trim() + "' and eas_no='" + tb_new_easno.Text.Trim() + "'";
                    if (main.dbtype == "SQL")
                    {
                        str_status = main.dbservice.Sql_exec_selectstring(str_sql);
                    }
                    else
                    {
                        str_status = main.dbservice.oracle_exec_selectstring(str_sql);
                    }
                    if (str_status == "1")
                    {
                        MessageBox.Show("单据已经分单,不能新增明细.");
                        return;
                    }

                    string str_customer;
                    w_so000_add wsingle = new w_so000_add();
                    str_customer = cbx_newcustomer.Text.Trim();
                    w_so000_add.str_qcustomer = str_customer;
                    w_so000_add.str_eas_no = tb_new_easno.Text.Trim();
                    wsingle.ShowDialog();
                    //f_get_detail();
                    f_display_detail(str_customer, tb_new_easno.Text.Trim());
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
        }

        private void btn_submodi_Click(object sender, EventArgs e)
        {
            if (dgv_detail.Rows.Count > 0)
            {
                try
                {
                    string str_sql = "", str_status = "";
                    str_sql = " select so_status from " + main.g_space + ".so_pre_ship_detail where inv_org='" + main.g_org + "' and consignor='" + cbx_newcustomer.Text.Trim() + "' and eas_no='" + tb_new_easno.Text.Trim() + "'";
                    if (main.dbtype == "SQL")
                    {
                        str_status = main.dbservice.Sql_exec_selectstring(str_sql);
                    }
                    else
                    {
                        str_status = main.dbservice.oracle_exec_selectstring(str_sql);
                    }
                    if (str_status == "1")
                    {
                        MessageBox.Show("单据已经分单,不能修改明细.");
                        return;
                    }

                    dgv_detail.ReadOnly = false;
                    for (int i = 0; i < dgv_detail.Rows.Count; i++)
                    {
                        dgv_detail.Rows[i].Cells["sku"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["sku_name"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["eas_line_no"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["order_unit"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["plan_cubage"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["plan_gross_weight"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["plan_net_weight"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["convert_modulus"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["consignor_"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute1"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute2"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute3"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute4"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute5"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute6"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute7"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute8"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute9"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute10"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute11"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute12"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute13"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute14"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["lot_attribute15"].ReadOnly = true;
                        dgv_detail.Rows[i].Cells["eas_no_"].ReadOnly = true;

                        dgv_detail.Rows[i].Cells["plan_so_qty_m"].ReadOnly = false;
                    }

                    f_setbutton("detail", false);

                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
            }
        }

        private void btn_subdel_Click(object sender, EventArgs e)
        {
            if (dgv_detail.Rows.Count > 0)
            {

                try
                {
                    string str_sql = "", str_status = "";
                    str_sql = " select so_status from " + main.g_space + ".so_pre_ship_detail where inv_org='" + main.g_org + "' and consignor='" + cbx_newcustomer.Text.Trim() + "' and eas_no='" + tb_new_easno.Text.Trim() + "'";
                    if (main.dbtype == "SQL")
                    {
                        str_status = main.dbservice.Sql_exec_selectstring(str_sql);
                    }
                    else
                    {
                        str_status = main.dbservice.oracle_exec_selectstring(str_sql);
                    }
                    if (str_status == "1")
                    {
                        MessageBox.Show("单据已经分单,不能删除明细.");
                        return;
                    }
                    if (MessageBox.Show("是否确认删除?", main.g_class.comm_getchangestring("Information", main.g_info_dataset), MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {

                        ArrayList al = new ArrayList();

                        for (int i = 0; i < dgv_detail.Rows.Count; i++)
                        {
                            if (dgv_detail.Rows[i].Cells["check_d"].Value.ToString().Trim() == "1")
                            {
                                str_sql = "delete " + main.g_space + ".so_pre_ship_detail   where inv_org='" + main.g_org + "' and consignor='" + cbx_newcustomer.Text.Trim() + "' and eas_no='" + tb_new_easno.Text.Trim() + "' and eas_line_no=" + dgv_detail.Rows[i].Cells["eas_line_no"].Value.ToString().Trim();
                            }
                            al.Add(str_sql);
                        }
                        string[] str_list = new string[al.Count];
                        for (int i = 0; i < al.Count; i++)
                        {
                            str_list[i] = al[i].ToString().Trim();
                        }
                        if (main.dbtype == "SQL")
                        {
                            main.dbservice.Sql_exec_mutilsql(str_list);
                        }
                        else
                        {
                            main.dbservice.oracle_exec_mutilsql(str_list);
                        }

                        f_display_detail(cbx_newcustomer.Text.Trim(), tb_new_easno.Text.Trim());
                    }
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show("保存失败" + ee.Message);
                }
            }
        }

        private void btn_subsave_Click(object sender, EventArgs e)
        {
            if (dgv_detail.Rows.Count > 0)
            {
                try
                {
                    decimal d_cubage = 0, d_gross_weight = 0, d_net_weight = 0, d_qty = 0, d_convert_modulus = 0, d_order_qty = 0, d_present = 0;
                    string str_sql = "";
                    string[] str_list = new string[dgv_detail.Rows.Count];
                    for (int i = 0; i < dgv_detail.Rows.Count; i++)
                    {
                        d_qty = 0;
                        d_cubage = Convert.ToDecimal(dgv_detail.Rows[i].Cells["cubage_sku"].Value.ToString().Trim());
                        d_gross_weight = Convert.ToDecimal(dgv_detail.Rows[i].Cells["gross_weight_sku"].Value.ToString().Trim());
                        d_net_weight = Convert.ToDecimal(dgv_detail.Rows[i].Cells["net_weight_sku"].Value.ToString().Trim());
                        d_qty = Convert.ToDecimal(dgv_detail.Rows[i].Cells["plan_so_qty_m"].Value.ToString().Trim());
                        d_order_qty = Convert.ToDecimal(dgv_detail.Rows[i].Cells["plan_so_qty_m"].Value.ToString().Trim());


                        d_convert_modulus = Convert.ToDecimal(dgv_detail.Rows[i].Cells["convert_modulus"].Value.ToString().Trim());

                        d_order_qty = Math.Round(d_order_qty * d_convert_modulus, 4);
                        d_present = Math.Round(d_present * d_convert_modulus, 4);
                        if (d_qty <= 0)
                        {
                            MessageBox.Show("出库数量不能为0");
                            {
                                return;
                            }
                        }
                        d_cubage = Math.Round(d_cubage * d_qty, 4);
                        d_gross_weight = Math.Round(d_gross_weight * d_qty, 4);
                        d_net_weight = Math.Round(d_net_weight * d_qty, 4);
                        str_sql = "update " + main.g_space + ".so_pre_ship_detail set plan_so_qty_m=" + dgv_detail.Rows[i].Cells["plan_so_qty_m"].Value.ToString().Trim() + ",plan_so_qty_s=" + d_order_qty.ToString().Trim() +
                                   " ,plan_cubage=" + d_cubage + ",plan_gross_weight=" + d_gross_weight + ",plan_net_weight= " + d_net_weight + ",update_time=getdate(),updater='" + main.g_user + "' " +
                                   " where inv_org='" + main.g_org + "' and consignor='" + cbx_newcustomer.Text.Trim() + "' and eas_no='" + tb_new_easno.Text.Trim() + "' and eas_line_no=" + dgv_detail.Rows[i].Cells["eas_line_no"].Value.ToString().Trim();
                        str_list[i] = str_sql;
                    }
                    if (main.dbtype == "SQL")
                    {
                        main.dbservice.Sql_exec_mutilsql(str_list);
                    }
                    else
                    {
                        main.dbservice.oracle_exec_mutilsql(str_list);
                    }
                    f_setbutton("detail", true);
                    dgv_detail.ReadOnly = true;
                    f_display_detail(cbx_newcustomer.Text.Trim(), tb_new_easno.Text.Trim());
                    btn_subsave.Enabled = false;
                    btn_subcancel.Enabled = false;
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show("保存失败" + ee.Message);
                }
            }
        }

        private void btn_subcancel_Click(object sender, EventArgs e)
        {
            f_setbutton("detail", true);
            dgv_detail.ReadOnly = true;
            f_display_detail(cbx_newcustomer.Text.Trim(), tb_new_easno.Text.Trim());
            btn_subsave.Enabled = false;
            btn_subcancel.Enabled = false;
        }



    }
}